##crack.py
from sys import *
from monocipher import *
from PolyAlphaCracker import *
from Breakhomophonic import *
from polygramCrack import *

if len(argv) < 4:
    print("error: run 'sudo python crack.py filetocrack mode outputfile")
    exit(0)


if argv[2] == '0':
    cipher = MonoCipher()
elif argv[2] == '1':
    cipher = VigCracker()
elif argv[2] == '2':
    cipher = Breakhomophonic()
elif argv[2] == '3':
   # print 'dont have polygramcipher cracker yet'
     cipher = crackPolygraphicCipher(4)
else:
    print 'error: cyphermode argument not recognized'
    exit(0)

FILE_READ = argv[1]
FILE_WRITE = argv[3]
reader = open(FILE_READ, 'r')
writer = open(FILE_WRITE, 'a')
writer.write('...NEW TRY AT CRACKING BELOW...\n\n')
text = None
try:
    for line in reader:
        text = line
        print 'trying to crack', text
        cracked_text = cipher.crack_string(text)
        writer.write(cracked_text)
        writer.write('\n')
except KeyboardInterrupt:
        print "Closing files and Exiting..."
        reader.close()
        writer.close()
        exit(0)
reader.close()
writer.close()
print 'Exiting crack program'
exit(0)
